package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.BaseController;
import com.bolt.admin.logging.annotation.LogAnnotation;
import com.bolt.admin.module.sys.dto.UserDTO;
import com.bolt.admin.module.sys.dto.converter.UserConverter;
import com.bolt.admin.module.sys.entity.UserEntity;
import com.bolt.admin.module.sys.service.UserService;
import com.bolt.common.utils.StrUtil;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.convention.data.Returns;
import com.bolt.core.view.components.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2018-02-26 16:46:13
 *         用户信息表
 **/
@Controller
@RequestMapping("/admin/sys/user")
public class UserController extends BaseController <UserEntity, String> {
    @Autowired
    private UserService userService;

    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/sys/user/index", request, model.asMap());
    }

    /**
     * 用户列表分页
     *
     * @return
     */
    @RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage page(Pageable pageable, UserDTO userDTO) {
        PageInfo <UserDTO> data = userService.findPage(pageable, userDTO);
        return Returns.success(data);
    }


    /**
     * 用户信息表新增操作
     *
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create() {
        return "/admin/sys/user/input-index";
    }

    /**
     * 用户信息表编辑页
     *
     * @param id    菜单ID
     * @param model
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(String id, Model model) {
        UserEntity user = userService.findOne(id).get();
        model.addAttribute(Form.PAGECONTEXT_FORMVALUES, UserConverter.toDTO(user));
        return "/admin/sys/user/input-index";
    }

    /**
     * 用户信息表新增操作
     *
     * @param userDTO 表单数据
     * @return
     */
    @LogAnnotation(module = "系统管理", description = "用户信息新增修改操作")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage saveOrUpdate(@Validated UserDTO userDTO) {
        userService.saveOrUpdate(userDTO);
        return Returns.success("保存成功");
    }

    /**
     * 用户信息表删除操作
     *
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    @LogAnnotation(module = "系统管理", description = "用户信息表删除操作")
    public ResultMessage doDelete(@RequestParam("id") String id) {
        if (StrUtil.isNotEmpty(id)) {
            userService.deleteById(id);
            return Returns.success("删除成功");
        } else {
            return Returns.error("所有选取记录删除操作失败");
        }
    }

    /**
     * 修改密码
     *
     * @param oldpasswd
     * @param newpasswd
     * @param cfmpassword
     * @return
     */
    @RequestMapping(value = "/password/reset", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage resetPasswordSave(@RequestParam("oldpasswd") String oldpasswd,
                                           @RequestParam("newpasswd") String newpasswd,
                                           @RequestParam("cfmpassword") String cfmpassword) {

        if (StrUtil.isEmpty(newpasswd)) {
            return Returns.warning("密码不能为空");
        }
        if (newpasswd.equals(cfmpassword)) {
            return Returns.warning("确定密码输入不正确");
        }
        //userService.modifyPassword(user, oldpasswd, newpasswd);
        return Returns.success("修改成功");

    }

    @RequestMapping(value = "userImageClip", method = RequestMethod.GET)
    public String userImageClip() {
        return "/admin/sys/user/user-imageclip";
    }

    @RequestMapping(value = "updateAvatarImg", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage updateAvatar(@RequestParam("avatarImg") MultipartFile avatarImg) {
        try {
            if (avatarImg.isEmpty()) {
                return Returns.warning("头像不能为空");
            }
            // userService.save(user);
            return Results.success("修改成功");
        } catch (Exception ex) {
            return Returns.error("头像修改失败");
        }
    }


}